package com.china08.yxyapi.api.comm;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass.YxyNotifictionUser;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class CompletionDataController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @Autowired
    RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    YxyNoticeClassRepository yxyNoticeClassRepository;

    @SuppressWarnings("unchecked")
    @ApiOperation(value = "补全云课程学校名称")
    @RequestMapping(value = "/api_yw/comm/cloud/lesson/schoolNick/upd", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void methos1() {
        System.out.println("补全云课程学校名称");
        List<YxyLessonsCloudLesson> list = lessonRepository.findAll();
        System.out.println("总课程：" + list.size());

        Set<String> schoolIdSet = new HashSet<>();

        for (YxyLessonsCloudLesson lesson : list) {
            String schoolId = lesson.getSchoolId();
            if (StringUtils.isNotBlank(schoolId)) {
                schoolIdSet.add(schoolId);
            }
        }

        for (String schoolId : schoolIdSet) {
            String url = yxyApiYxUrl + "/comm/school/all/" + schoolId;
            Map<String, Object> schoolMap = restTemplate.getForObject(url, Map.class);
            if (schoolMap == null) {
                continue;
            }
            String schoolNick = schoolMap.get("schoolNick") + "";
            //
            List<YxyLessonsCloudLesson> lessons = lessonRepository.findBySchoolId(schoolId);
            for (YxyLessonsCloudLesson le : lessons) {
                if (null != le && StringUtils.isNotBlank(schoolNick)) {
                    le.setSchoolNick(schoolNick);
                }
            }
            lessonRepository.save(lessons);
            System.out.println("修改成功");
        }
    }

    @ApiOperation(value = "班级通知计算接收率")
    @RequestMapping(value = "/api_yw/comm/class/notice/receive/rete", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void methos2() {
        try {
            System.out.println("班级通知计算接收率");
            // 总条数
            long count = yxyNoticeClassRepository.count();
            System.out.println("总条数" + count);
            if (count == 0) { return; }

            long allPage = ((count % 20 == 0) ? (count / 20) : (count / 20 + 1));
            System.out.println("总叶数" + allPage);
            for (int i = 0; i < allPage; i++) {

                Page<YxyNoticeClass> yxyNoticeClassLst = yxyNoticeClassRepository.findAll(new PageRequest(i, 20));

                for (YxyNoticeClass yxyNoticeClass : yxyNoticeClassLst) {
                    List<YxyNotifictionUser> yxyNotifictionUserList = yxyNoticeClass.getReceiver();
                    if (yxyNotifictionUserList.size() == 0) {
                        yxyNoticeClass.setReceiveRate(0.0);
                    } else {
                        Integer allReaded = 0;
                        Integer allSize = yxyNotifictionUserList.size();
                        for (YxyNotifictionUser user : yxyNotifictionUserList) {
                            if (user.isRead == 1) {
                                allReaded = allReaded + 1;
                            }
                        }
                        double i1 = allReaded;
                        double i2 = allSize;
                        double receiveRate = i1 / i2;
                        yxyNoticeClass.setReceiveRate(receiveRate);
                    }
                    System.out.println("接收率：" + yxyNoticeClass.getReceiveRate());
                    yxyNoticeClassRepository.save(yxyNoticeClass);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
